package com.zmn.mcc.admin.controller.role.staff;

import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.model.entity.role.McRole;
import com.zmn.mcc.model.entity.staff.McStaff;
import com.zmn.mcc.model.entity.staff.McStaffQuery;
import com.zmn.mcc.model.vo.McStaffVO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/05/11 19:48
 */
@Controller
@RequestMapping("/role/staff/")
public class RoleStaffListController extends RoleStaffBaseController {

    /**
     * 员工列表
     */
    @RequestMapping(value = "list")
    public String list(HttpServletRequest request,
                       @ModelAttribute McStaffQuery query,
                       Model model) {

        String keyword = request.getParameter("keyword");
        //if (NumberUtil.isNumber(keyword) && keyword.length() <= 8) {
        //    query.setStaffId(Integer.parseInt(keyword));
        //    query.setStaffName("");
        //} else {
        //    query.setStaffName(keyword);
        //}
        if(NumberUtil.isNullOrZero(query.getRoleId())) {
            query.setRoleId(-1);
        }

        List<McStaffVO> items = staffService.listPageInfoByQuery(query);

        McRole obj = !NumberUtil.isNullOrZero(query.getRoleId()) ? roleService.findByKey(query.getRoleId()) : new McRole();

        model.addAttribute("items", items);
        model.addAttribute("vo", query);

        model.addAttribute("obj", obj);

        model.addAttribute("keyword", keyword);
        //model.addAttribute("yesno", GlobalDict.getYesOrNoList());
        model.addAttribute("statuses", GlobalDict.getStatusList(true));

        // 是否拥有"权限管理员角色"
        model.addAttribute("isAssignAdmin", staffBService.isAssignAdmin(getStaffId()));

        return "role/staff/list";
    }
}
